const { baseUrl } = require("../../../utils/util")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    current:1,
    codeText:'获取验证码',
    counting:false,
    phone:'',
    passwrod:'',
    passwrodset1:'',
    nickname:'',
    code:'',
  },
  bindPhone(e){
    console.log(e)
    this.setData({
      phone:e.detail.value
    })
  },
  bindPassword(e){
    this.setData({
      passwrod:e.detail.value
    })
  },
  bindCode(e){
    this.setData({
      code:e.detail.value
    })
  },
  bindPasswordSet_1(e){
    this.setData({
      passwrodset1:e.detail.value
    })
  },
  bindnikename(e){
    this.setData({
      nickname:e.detail.value
    })
  },
  // 登陆注册监听
  click(e){
    let index = e.currentTarget.dataset.code;
    this.setData({
      current:index
    })
  },
  loginOrrigester(){
    var that =this
    if (this.data.current==1) {
      console.log("这是登陆")
      wx.request({
        // url: localUrl+'/ucenter/member/login',
        url:baseUrl+'/ucenter/member/login',
        // https://wechat.lovegcu.xyz/ucenter/member/login
        method:'POST',
        data:{
          mobile:this.data.phone,
          password:this.data.passwrod,
        },
        success(res){
          if (res.data.code==20000) {
            wx.showToast({
              title: res.data.data.message,
            })
            wx.setStorageSync('token', res.data.data.token)
            wx.reLaunch({
              url: '../my/my',
            })
          }else
          {
            wx.showToast({
              title: '登陆失败',
              icon:'error'
            })
          }
         
          console.log(res)
        }
        
      })
    }

    if (this.data.current==0) {

      if (this.data.passwordset1!==this.data.passwordset2) {
        wx.showToast({
          title: '两次密码不一致',
          icon:'error'
        })
      }
      wx.request({
        url: baseUrl+'/ucenter/member/register',
        method:'POST',
        data:{
          code: this.data.code,
          mobile:this.data.phone,
          nickname: this.data.nickname,
          password: this.data.passwrodset1
        },
        success(res){
          console.log(res)
          wx.showToast({
            title: res.data.message,
          })
          if (res.data.code==20000) {
            that.setData({
              current:1
            })
       
          }
        }
      })
      console.log("这是注册")
    }
  },
  //获取验证码 
  getCode(){
    var that = this;
   
    if (!that.data.counting) {
      wx.request({
        url: baseUrl+'/wechatmsm/msm/send/'+that.data.phone,
        method:'GET'
      })
      wx.showToast({
        title: '验证码已发送',
      })
      //开始倒计时60秒
      that.countDown(that, 300);
    } 
  },
  //倒计时60秒
  countDown(that,count){
    if (count == 0) {
      that.setData({
        codeText: '获取验证码',
        counting:false
      })
      return;
    }
    that.setData({
      counting:true,
      codeText: count + '秒后重新获取',
    })
    setTimeout(function(){
      count--;
      that.countDown(that, count);
    }, 1000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
     
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
